//Our includes
#include "Station.h"
#include "Survey.h"

//Std includes
#include <list>



/*!
  \brief Constructor - Initilizes all the data in station

*/

Station::Station() {
    ParentSurvey = NULL;
} //end Station()

/*!
    Deconstruct
 */
Station::~Station() {


} //~Station


/*!
\brief Adds a Leg to the station
*/
void Station::AddLeg(Leg* Leg) {
    Legs.push_back(Leg);
}

/*!
\brief Removes a Leg from the station
This runs in linear time.  The Leg is NOT delete
from memory.

\param Leg - The Leg that will be removed from this station
*/
void Station::RemoveLeg(Leg* leg) {
    std::vector<Leg*>::iterator iter; // = Legs.begin();
    for(iter = Legs.begin(); iter != Legs.end(); iter++) {
        Leg* currentLeg = *iter;
        if(currentLeg == leg) {
            Legs.erase(iter);
        }
        break;
    }
}

/*!
  \brief Linearly searchs for a Leg and return's it's index
\param Leg - The Leg
\return The index of the Leg in the station
    */
int Station::IndexOf(Leg* Leg) {
    for(int i = 0; i < GetNumberOfLegs(); i++) {
        if(Leg == GetLeg(i)) {
            return i;
        }
    }
    return -1;
}

/*!
\brief Linearly Find if this station has this Leg
\param Leg - the Leg your looking for in the Leg
\return true if the Leg exist in the station or false if it's not in
the station
  */
bool Station::HasLeg(Leg* Leg) {
    if(IndexOf(Leg) < 0) {
        return false;
    }
    return true;
}

/*!
       \brief Get the full station name, including the survey name

       The survey names will be added to as a prefix to the station
       name seperated by a period.  If the parent survey name
       is empty then the survey will be omitted.

       For example:
       Station1 is owned by survey2 and survey1 owns survey2.
       survey1->survey2->survey3->station1
       Survey1 name = "Dark Cave"
       Survey2 name = ""
       Survey3 name = "Area51"
       Station1 name = "A10"
       Then the fullanem is "Area51.Dark Cave.A10"

       This is extremely useful for connecting two caves / surveyes
       and looking up a station by it's full name. See Network::FindStation()
      */
std::string Station::GetFullName() {

    std::string fullName;

    //Stores all the survey names as we traverse up
    std::list<std::string> surveyNames;

    //Go through all the parent surveys
    Survey* currentSurvey = GetSurvey();
    while(currentSurvey != NULL) {
        //Build a list of names so we can build the names
        std::string currentName = currentSurvey->GetName();

        if(currentName.size() > 0) {
            surveyNames.push_back(currentName);
        }
    }

    //Go through the names and build the full name
    std::list<std::string>::reverse_iterator iter;
    for(iter = surveyNames.rbegin(); iter != surveyNames.rend();) {
        std::string currentName = *iter;

        fullName.append(currentName);

        iter++;
        if(surveyNames.rend() != iter) {
            fullName.append(".");
        }
    }

    //Add this station name
    fullName.append(Name);

    return fullName;
}
